package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.ZoneService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/zones")
public class ZoneController extends BaseController {

    @Autowired
    private ZoneService zoneService;

    /**
     * 获取全部地块信息
     * @return
     */
    @GetMapping
    public Result getAll(){
        List<Zone> all = null;
        try {
            all = zoneService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_QUERY_SUCCESS, all);
    }

    /**
     * 导出地块信息到 Excel表格
     * @param response
     * @param zone
     */
    @Log(title = "地块管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Zone zone) {
        List<Zone> list = zoneService.getList(zone);
        ExcelUtil<Zone> util = new ExcelUtil<Zone>(Zone.class);
        util.exportExcel(response, list, "地块数据");
    }

    /**
     * 从Excel表格导入地块数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "地块管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Zone> util = new ExcelUtil<Zone>(Zone.class);
        List<Zone> zoneList = util.importExcel(file.getInputStream());
        String message = zoneService.importZone(zoneList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入Excel模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Zone> util = new ExcelUtil<Zone>(Zone.class);
        util.importTemplateExcel(response, "地块数据");
    }

    /**
     * 带搜索功能分页获取地块信息
     * @param zone
     * @return
     */
    @GetMapping("/page")
    public PageResult getList(Zone zone){
        QueryPageUtils.startPage();
        List<Zone> allZone = null;
        PageInfo pageInfo;
        try {
            allZone = zoneService.getList(zone);
            pageInfo = new PageInfo<>(allZone);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.ZONE_QUERY_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS, pageInfo.getTotal(), MessageConstant.ZONE_QUERY_SUCCESS, allZone);
    }

    /**
     * 根据地块 id 获取地块信息
     * @param zoneId
     * @return
     */
    @GetMapping("/{zoneId}")
    public Result getById(@PathVariable int zoneId){
        Zone byId = null;
        try {
            byId = zoneService.getById(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_QUERY_SUCCESS, byId);
    }

    /**
     * 新增一个地块
     * @param zone
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Zone zone) {
        try {
            zoneService.add(zone);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_ADD_SUCCESS);
    }

    /**
     * 修改某个地块信息
     * @param zone
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Zone zone){
        try {
            zoneService.update(zone);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_UPDATE_SUCCESS);
    }

    /**
     * 根据 id 删除某个地块
     * @param zoneId
     * @return
     */
    @DeleteMapping("{zoneId}")
    public Result delete(@PathVariable int zoneId){
        try {
            zoneService.delete(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_DELETE_SUCCESS);
    }
}
